package com.smart.parking.controller.backstage;

import com.smart.parking.config.Result;
import com.smart.parking.entity.UserEntity;
import com.smart.parking.service.UserService;
import com.smart.parking.utils.PageUtils;
import com.smart.parking.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 用户信息表
 *
 * @author ciyun
 * @email ciyun@gmail.com
 * @date 2022-10-30 14:12:10
 */
@RestController
@RequestMapping("/back/user")
public class UserController {
    @Autowired
    private UserService userService;

    //定义日志
    private final static Logger log = LoggerFactory.getLogger(UserController.class);

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return Result.ok().put("page", page);
    }

    /**
     * 发送手机验证码
     *
     * @return {@link Result}
     */
    @GetMapping("/sendMsgPhone")
    public Result sendMsgPhone(@RequestParam("phone") String phone) {
        boolean flag = userService.senMsgPhone(phone);
        if (flag) {
            return Result.ok("发送成功!");
        }
        return Result.error("发送失败!");
    }

    /**
     * 登录用户
     *
     * @param userName 用户名
     * @param passWord 密码
     * @return {@link Result}
     */
    @GetMapping("/userLogin")
    public Result loginUser(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
        boolean flag = userService.userLogin(userName, passWord);
        if (flag) {
            log.info("登录成功!");
            return Result.ok("登录成功!");
        }
        log.info("登录失败!");
        return Result.error("登录失败!");
    }

    /**
     * 用户手机登录
     *
     * @param phone 手机号
     * @param code  代码
     * @return {@link Result}
     */
    @GetMapping("/userPhoneLogin")
    public Result userPhoneLogin(@RequestParam("phone") String phone, @RequestParam("code") String code) {
        boolean flag = userService.userPhoneLogin(phone, code);
        if (flag) {
            return Result.ok("登录成功!");
        }
        return Result.error("登录失败!");
    }

    /**
     * 用户注册
     *
     * @param userVO 用户
     * @return {@link Result}
     */
    @PostMapping("/userRegister")
    public Result userRegister(@RequestBody UserVO userVO) {
        boolean flag = userService.userRegister(userVO);
        if (flag) {
            return Result.ok("注册成功!");
        }
        return Result.error("注册失败!");
    }

    /**
     * 用户更新
     *
     * @param userEntity 用户实体
     * @return {@link Result}
     */
    @PutMapping("/userUpdate")
    public Result userUpdate(@RequestBody UserEntity userEntity) {
        boolean flag = userService.userUpdate(userEntity);
        if (flag) {
            return Result.ok("更新成功!");
        }
        return Result.error("更新失败!");

    }

    /**
     * 用户删除
     *
     * @param userId 用户id
     * @return {@link Result}
     */
    @DeleteMapping("/userDelete")
    public Result userDelete(@RequestParam("userId") Long userId) {
        boolean flag = userService.removeById(userId);
        if (flag) {
            return Result.ok("删除成功!");
        }
        return Result.error("删除失败!");

    }

    /**
     * 用户批量删除
     *
     * @param userIds 用户id列表
     * @return {@link Result}
     */
    @DeleteMapping("/userDeletes")
    public Result userDeletes(@RequestBody Long[] userIds) {
        userService.removeByIds(Arrays.asList(userIds));

        return Result.ok();
    }


}
